#include "mysql.h"
#include "QSqlDatabase"
#include "QSqlQuery"
#include "QSqlError"
#include "dialog_msbox.h"

mysql::mysql()
{

}



int mysql::SYSTEM_LOAD()//创建数据库与表
{
    //判断数据库文件是否存在，如果存在就删除
    QDir dirfile;
    dirfile.mkpath(QCoreApplication::applicationDirPath()+"/myDb");//创建保存目录
    QString file_path=QCoreApplication::applicationDirPath()+QString("/myDb/system_data.db");
    if(dirfile.exists(file_path))
     {
        QFile file(file_path);
        file.remove();
     }

    //创建数据库文件
    QSqlDatabase database;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
      database = QSqlDatabase::database("qt_sql_default_connection");
    else
      database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName(file_path);

    //创建数据库表结构

    if(database.open())
    {
        QSqlQuery sql_query(database);

        //创建表----test_system
        qDebug() << "The will creat table test_system !";
        if(!sql_query.exec("CREATE TABLE [test_system]("
                           "[system_pass] VARCHAR(255) NULL,"         //<管理员密码
                           "[system_user] VARCHAR(255) NULL,"         //<注册实验室
                           "[system_jrs]  VARCHAR(255) NULL,"         //<加热丝热值
                           "[system_bjs]  VARCHAR(255) NULL,"         //<苯甲酸热值
                           "[system_tjw1] VARCHAR(255) NULL,"         //<添加物热值1
                           "[system_tjw2] VARCHAR(255) NULL,"         //<添加物热值2
                           "[system_rrl1] VARCHAR(255) NULL,"         //<当前热容量
                           "[system_rrl2] VARCHAR(255) NULL,"         //<修正热容量
                           "[system_zsti] VARCHAR(255) NULL,"         //<注水时间
                           "[system_psti] VARCHAR(255) NULL,"         //<排水时间
                           "[system_com]  VARCHAR(255) NULL,"         //<串口名称
                           "[system_tcp]  VARCHAR(255) NULL,"         //<IP地址
                           "[system_port] VARCHAR(255) NULL)"))       //<端口号
        {
            qDebug() << "Error: Fail to create table test_system."<< sql_query.lastError();
        }
        else
        {
            qDebug() << "Table test_system created!";

            //创建表----test_info
            qDebug() << "The will creat table test_info !";

            if(!sql_query.exec("CREATE TABLE [test_info]("
                               "[test_id]    VARCHAR(255) NULL,"         //<试样编号
                               "[test_md]    VARCHAR(255) NULL,"         //<试样密度
                               "[test_hd]    VARCHAR(255) NULL,"         //<试样厚度
                               "[test_gz]    VARCHAR(255) NULL,"         //<试样构造
                               "[test_tj]    VARCHAR(255) NULL,"         //<状态调节
                               "[test_Ddate] VARCHAR(255) NULL,"         //<到样日期
                               "[test_scdm]  VARCHAR(255) NULL,"         //<生产单位
                               "[test_sjdm]  VARCHAR(255) NULL,"         //<送检单位
                               "[test_syxx]  VARCHAR(255) NULL,"         //<试验现象
                               "[test_sybz]  VARCHAR(255) NULL)"))       //<试验备注
            {
                qDebug() << "Error: Fail to create table test_info."<< sql_query.lastError();
            }
            else
            {
                qDebug() << "Table test_info created!";

                //创建表----test_data
                qDebug() << "The will creat table test_data !";

                if(!sql_query.exec("CREATE TABLE [test_data]("
                                   "[test_id]  VARCHAR(255) NULL,"         //<试样ID
                                   "[test_index]  VARCHAR(255) NULL,"      //<试样序号
                                   "[data_t1]  VARCHAR(255) NULL,"         //<起点温度
                                   "[data_t2]  VARCHAR(255) NULL,"         //<终止温度
                                   "[data_sum] VARCHAR(255) NULL,"         //<总热值
                                   "[data_tjw] VARCHAR(255) NULL,"         //<添加物热值
                                   "[data_bjs] VARCHAR(255) NULL,"         //<苯甲酸热值
                                   "[data_jrs] VARCHAR(255) NULL,"         //<发热丝热值
                                   "[data_dtl] VARCHAR(255) NULL,"         //<弹筒热值
                                   "[data_sdl] VARCHAR(255) NULL,"         //<水当量热值
                                   "[data_zp]  VARCHAR(255) NULL,"         //<制品热值
                                   "[data_d1]  VARCHAR(255) NULL,"         //<开始时间
                                   "[data_d2]  VARCHAR(255) NULL)"))       //<结束时间
                {
                    qDebug() << "Error: Fail to create table test_data."<< sql_query.lastError();
                }
                else
                {
                     qDebug() << "Table test_data created!";

                     qDebug() << "The will creat table test_temp !";

                     if(!sql_query.exec("CREATE TABLE [test_temp]("
                                        "[test_id]     VARCHAR(255) NULL,"     //<试样ID
                                        "[test_index]  VARCHAR(255) NULL,"     //<试样序号
                                        "[test_ti]     VARCHAR(255) NULL,"     //<启动第N秒
                                        "[test_tk]     VARCHAR(255) NULL,"     //<温度
                                        "[test_tn]     VARCHAR(255) NULL)"))   //<记录时间
                     {
                         qDebug() << "Error: Fail to create table test_data."<< sql_query.lastError();
                     }
                     else
                     {
                         qDebug() << "Table test_temp created!";
                     }
                }


            }
        }

        //关闭数据库
         database.close();
    }

    QSqlDatabase::removeDatabase("QSQLITE");//关闭驱动连接
    return 1;
}

int mysql::INSERT_system(test_system data)//保存系统参数
{
    QDir dirfile;
    QString file_path=QCoreApplication::applicationDirPath()+QString("/myDb/system_data.db");
    if(dirfile.exists(file_path))
     {
           QSqlDatabase database;
           if(QSqlDatabase::contains("qt_sql_default_connection"))
             database = QSqlDatabase::database("qt_sql_default_connection");
           else
             database = QSqlDatabase::addDatabase("QSQLITE");
           database.setDatabaseName(file_path);

           if(database.open())
           {

               QSqlQuery sql_query(database);

               //先清除，在插入
               if(sql_query.exec("DELETE FROM test_system"))
                   qDebug() << "DELETE test_system is ok!";

                QString insert_sql ="INSERT INTO test_system (system_pass,system_user,system_jrs,system_bjs,system_tjw1,system_tjw2"
                                    ",system_rrl1,system_rrl2,system_zsti,system_psti,system_com,system_tcp,system_port) "
                 "VALUES (:system_pass,:system_user,:system_jrs,:system_bjs,:system_tjw1,:system_tjw2"
                                    ",:system_rrl1,:system_rrl2,:system_zsti,:system_psti,:system_com,:system_tcp,:system_port)";
                sql_query.prepare(insert_sql);
                sql_query.bindValue(":system_pass", QVariant(data.system_pass));
                sql_query.bindValue(":system_user", QVariant(data.system_user));
                sql_query.bindValue(":system_jrs", QVariant(data.system_jrs));
                sql_query.bindValue(":system_bjs", QVariant(data.system_bjs));
                sql_query.bindValue(":system_tjw1", QVariant(data.system_tjw1));
                sql_query.bindValue(":system_tjw2", QVariant(data.system_tjw2));
                sql_query.bindValue(":system_rrl1", QVariant(data.system_rrl1));
                sql_query.bindValue(":system_rrl2", QVariant(data.system_rrl2));
                sql_query.bindValue(":system_zsti", QVariant(data.system_zsti));
                sql_query.bindValue(":system_psti", QVariant(data.system_psti));
                sql_query.bindValue(":system_com", QVariant(data.system_com));
                sql_query.bindValue(":system_tcp", QVariant(data.system_tcp));
                sql_query.bindValue(":system_port", QVariant(data.system_port));

                if(sql_query.exec())
                     qDebug() << "INSERT test_system is ok!";


           }

           database.close();
           QSqlDatabase::removeDatabase("QSQLITE");//关闭驱动连接




     }

    return 1;
}

int mysql::INSERT_test_data(test_data data)//插入与更新试验数据
{

    QDir dirfile;
    QString file_path=QCoreApplication::applicationDirPath()+QString("/myDb/system_data.db");
    if(dirfile.exists(file_path))
     {
           QSqlDatabase database;
           if(QSqlDatabase::contains("qt_sql_default_connection"))
             database = QSqlDatabase::database("qt_sql_default_connection");
           else
             database = QSqlDatabase::addDatabase("QSQLITE");
           database.setDatabaseName(file_path);

           if(database.open())
           {

               QSqlQuery sql_query(database);

               //先清除，在插入
                QString delete_sql=QString("DELETE FROM test_data where test_id='%1' and test_index='%2'")
                        .arg(data.data_id).arg(data.data_index);
                if(sql_query.exec(delete_sql))
                   qDebug() << "DELETE test_data is ok!";

                QString insert_sql ="INSERT INTO test_data (test_id,test_index,data_t1,data_t2,data_sum,data_tjw"
                                    ",data_bjs,data_jrs,data_dtl,data_sdl,data_zp,data_d1,data_d2) "
                 "VALUES (:test_id,:test_index,:data_t1,:data_t2,:data_sum,:data_tjw"
                                    ",:data_bjs,:data_jrs,:data_dtl,:data_sdl,:data_zp,:data_d1,:data_d2)";

                sql_query.prepare(insert_sql);
                sql_query.bindValue(":test_id", QVariant(data.data_id));
                sql_query.bindValue(":test_index", QVariant(data.data_index));
                sql_query.bindValue(":data_t1", QVariant(data.data_t1));
                sql_query.bindValue(":data_t2", QVariant(data.data_t2));
                sql_query.bindValue(":data_sum", QVariant(data.data_sum));
                sql_query.bindValue(":data_tjw", QVariant(data.data_tjw));
                sql_query.bindValue(":data_bjs", QVariant(data.data_bjs));
                sql_query.bindValue(":data_jrs", QVariant(data.data_jrs));
                sql_query.bindValue(":data_dtl", QVariant(data.data_dtl));
                sql_query.bindValue(":data_sdl", QVariant(data.data_sdl));
                sql_query.bindValue(":data_zp", QVariant(data.data_zp));
                sql_query.bindValue(":data_d1", QVariant(data.data_d1));
                sql_query.bindValue(":data_d2", QVariant(data.data_d2));
                sql_query.bindValue(":data_sum", QVariant(data.data_sum));
                sql_query.bindValue(":data_tjw", QVariant(data.data_tjw));

                if(sql_query.exec())
                     qDebug() << "INSERT test_data is ok!";

           }

           database.close();
           QSqlDatabase::removeDatabase("QSQLITE");//关闭驱动连接


     }
    return 1;
}
int mysql::INSERT_test_info(test_info info)//插入与更新试验样品信息
{

    QDir dirfile;
    QString file_path=QCoreApplication::applicationDirPath()+QString("/myDb/system_data.db");
    if(dirfile.exists(file_path))
     {
           QSqlDatabase database;
           if(QSqlDatabase::contains("qt_sql_default_connection"))
             database = QSqlDatabase::database("qt_sql_default_connection");
           else
             database = QSqlDatabase::addDatabase("QSQLITE");
           database.setDatabaseName(file_path);

           if(database.open())
           {

               QSqlQuery sql_query(database);

               //先清除，在插入
                QString delete_sql=QString("DELETE FROM test_info where test_id='%1'").arg(info.test_id);
                if(sql_query.exec(delete_sql))
                   qDebug() << "DELETE test_info is ok!";

                QString insert_sql ="INSERT INTO test_info (test_id,test_md,test_hd,test_gz,test_tj,test_Ddate,test_scdm"
                                    ",test_sjdm,test_syxx,test_sybz) "
                 "VALUES (:test_id,:test_md,:test_hd,:test_gz,:test_tj,:test_Ddate,:test_scdm"
                                    ",:test_sjdm,:test_syxx,:test_sybz)";

                sql_query.prepare(insert_sql);
                sql_query.bindValue(":test_id", QVariant(info.test_id));
                sql_query.bindValue(":test_md", QVariant(info.test_md));
                sql_query.bindValue(":test_hd", QVariant(info.test_hd));
                sql_query.bindValue(":test_gz", QVariant(info.test_gz));
                sql_query.bindValue(":test_tj", QVariant(info.test_tj));
                sql_query.bindValue(":test_Ddate", QVariant(info.test_Ddate));
                sql_query.bindValue(":test_scdm", QVariant(info.test_scdm));
                sql_query.bindValue(":test_sjdm", QVariant(info.test_sjdm));
                sql_query.bindValue(":test_syxx", QVariant(info.test_syxx));
                sql_query.bindValue(":test_sybz", QVariant(info.test_sybz));

                if(sql_query.exec())
                {
                    qDebug() << "INSERT test_info is ok!";

                    //初始化试验数据

                    if(sql_query.exec(QString("select * from test_data where test_id='%1'").arg(info.test_id)))
                    {
                        if(!sql_query.first())
                        {
                            qDebug()<<"没有查到试验数据,判断为新增，开始初始化数据";
                            for(int i=1;i<7;i++)
                            {
                                QString sql_insert=QString("INSERT INTO test_data VALUES('%1','%2','','','','','','','','','','','')")
                                        .arg(info.test_id).arg(i);

                                if(sql_query.exec(sql_insert))
                                    qDebug()<<QString("已完成初始化第 %1次数据 ……").arg(i);

                            }

                        }
                    }

                }

           }

           database.close();
           QSqlDatabase::removeDatabase("QSQLITE");//关闭驱动连接

           int ret=Dialog_msbox::show(0,0,"信息保存","Information saving","试验样品数据保存完成。");


     }
    return 1;
}

int mysql::query_system(test *buffer)//查询系统参数
{
    QDir dirfile;
    QString file_path=QCoreApplication::applicationDirPath()+QString("/myDb/system_data.db");
    if(dirfile.exists(file_path))
     {
        QSqlDatabase database;
        if(QSqlDatabase::contains("qt_sql_default_connection"))
          database = QSqlDatabase::database("qt_sql_default_connection");
        else
          database = QSqlDatabase::addDatabase("QSQLITE");
        database.setDatabaseName(file_path);

           if(database.open())
           {

               QSqlQuery sql_query(database);
               if(sql_query.exec("select system_pass,system_user,system_jrs,system_bjs,system_tjw1,system_tjw2"
                                 ",system_rrl1,system_rrl2,system_zsti,system_psti,system_com,system_tcp,"
                                 "system_port from test_system"))
               {
                   qDebug() << "select test_system is ok!";

                    sql_query.first();
                    buffer->system.system_pass=sql_query.value(0).toString();
                    buffer->system.system_user=sql_query.value(1).toString();
                    buffer->system.system_jrs=sql_query.value(2).toDouble();
                    buffer->system.system_bjs=sql_query.value(3).toDouble();
                    buffer->system.system_tjw1=sql_query.value(4).toDouble();
                    buffer->system.system_tjw2=sql_query.value(5).toDouble();
                    buffer->system.system_rrl1=sql_query.value(6).toDouble();
                    buffer->system.system_rrl2=sql_query.value(7).toDouble();
                    buffer->system.system_zsti=sql_query.value(8).toInt();
                    buffer->system.system_psti=sql_query.value(9).toInt();
                    buffer->system.system_com=sql_query.value(10).toString();
                    buffer->system.system_tcp=sql_query.value(11).toString();
                    buffer->system.system_port=sql_query.value(12).toString();
               }
               else
                   qDebug() << "Error: Fail to select table test_system."<< sql_query.lastError();

           }else
           {
                 qDebug() << "Error: open table test_system.";
           }

           database.close();
           QSqlDatabase::removeDatabase("QSQLITE");//关闭驱动连接


     }
    return 1;
}

QList<QStringList> mysql::query_test_info(QString test_id,QString test_date)//查询试验索引与结查
{
    QDir dirfile;
    QList<QStringList> sql_data;
    QString file_path=QCoreApplication::applicationDirPath()+QString("/myDb/system_data.db");
    if(dirfile.exists(file_path))
     {
        QSqlDatabase database;
        if(QSqlDatabase::contains("qt_sql_default_connection"))
          database = QSqlDatabase::database("qt_sql_default_connection");
        else
          database = QSqlDatabase::addDatabase("QSQLITE");
        database.setDatabaseName(file_path);

           if(database.open())
           {
               QSqlQuery sql_query(database);
               QStringList test_idtr;
               test_idtr.clear();
               QString sql_str="select distinct test_id from test_data where test_id like '%" + QString("%1").arg(test_id)+"%'"
                       +" and data_d1 like '%"+QString("%1").arg(test_date)+"%'";
               qDebug()<<sql_str;
               if(sql_query.exec(sql_str))
               {
                   if(sql_query.first())
                   {
                       test_idtr.append(sql_query.value(0).toString());
                       qDebug()<<sql_query.value(0).toString();

                       while(sql_query.next())
                       {
                           test_idtr.append(sql_query.value(0).toString());
                           qDebug()<<sql_query.value(0).toString();
                       }

                   }

                   if(test_idtr.count()>0)
                   {
                       for(int i=0;i<test_idtr.count();i++)
                       {
                            //添加试验编号
                            QStringList sql_1;
                            sql_1.append(test_idtr.at(i));

                            QString sql_str=QString("select "
                                                    "sum(case when test_index='1' then data_zp end )as index_1,"
                                                    "sum(case when test_index='2' then data_zp end )as index_2,"
                                                    "sum(case when test_index='3' then data_zp end )as index_3,"
                                                    "sum(case when test_index='4' then data_zp end )as index_4,"
                                                    "sum(case when test_index='5' then data_zp end )as index_5,"
                                                    "sum(case when test_index='6' then data_zp end )as index_6,"
                                                    "AVG(data_zp)as avg ,MAX(data_d2)as da "
                                                    "from test_data where test_id='%1'").arg(test_idtr.at(i));

                            if(sql_query.exec(sql_str))
                            {
                                if(sql_query.first())
                                {
                                    for(int j=0;j<8;j++)sql_1.append(sql_query.value(j).toString());
                                    if(sql_query.exec(QString("select * from test_info where test_id='%1'").arg(test_idtr.at(i))))
                                    {

                                        QString ToolTip;
                                        if(sql_query.first())
                                        {
                                            ToolTip=ToolTip+QString("试样编号=%1 \n").arg(sql_query.value(0).toString());
                                            ToolTip=ToolTip+QString("试样密度=%1 \n").arg(sql_query.value(1).toString());
                                            ToolTip=ToolTip+QString("试样厚度=%1 \n").arg(sql_query.value(2).toString());
                                            ToolTip=ToolTip+QString("试样构造=%1 \n").arg(sql_query.value(3).toString());
                                            ToolTip=ToolTip+QString("状态调节=%1 \n").arg(sql_query.value(4).toString());
                                            ToolTip=ToolTip+QString("到样日期=%1 \n").arg(sql_query.value(5).toString());
                                            ToolTip=ToolTip+QString("生产单位=%1 \n").arg(sql_query.value(6).toString());
                                            ToolTip=ToolTip+QString("送检单位=%1 \n").arg(sql_query.value(7).toString());
                                            //ToolTip=ToolTip+QString("试验现象=%1 \n").arg(sql_query.value(8).toString());
                                            //ToolTip=ToolTip+QString("试验备注=%1").arg(sql_query.value(9).toString());
                                            sql_1.append(ToolTip);
                                            qDebug()<<"add test_info"<<sql_1.count();

                                        }
                                    }
                                }

                            }

                            sql_data.append(sql_1);

                       }
                   }


               }


           }else
           {
                 qDebug() << "Error: open table test_system.";
           }

           database.close();
           QSqlDatabase::removeDatabase("QSQLITE");//关闭驱动连接
     }

    return sql_data;

}
int mysql::query_test_all(QString test_id,test* buffer)//加载试验所有数据
{
    QDir dirfile;
    QString file_path=QCoreApplication::applicationDirPath()+QString("/myDb/system_data.db");
    if(dirfile.exists(file_path))
     {
        QSqlDatabase database;
        if(QSqlDatabase::contains("qt_sql_default_connection"))
          database = QSqlDatabase::database("qt_sql_default_connection");
        else
          database = QSqlDatabase::addDatabase("QSQLITE");
        database.setDatabaseName(file_path);

           if(database.open())
           {

               QSqlQuery sql_query(database);
               QString sql_info=QString("select test_id,test_md,test_hd,test_gz,test_tj,test_Ddate,test_scdm,test_sjdm,"
                                        "test_syxx,test_sybz from test_info where test_id='%1'").arg(test_id);
               if(sql_query.exec(sql_info))
               {
                   if(sql_query.first())
                   {
                       buffer->info.test_id=sql_query.value(0).toString();
                       buffer->info.test_md=sql_query.value(1).toString();
                       buffer->info.test_hd=sql_query.value(2).toString();
                       buffer->info.test_gz=sql_query.value(3).toString();
                       buffer->info.test_tj=sql_query.value(4).toString();
                       buffer->info.test_Ddate=sql_query.value(5).toString();
                       buffer->info.test_scdm=sql_query.value(6).toString();
                       buffer->info.test_sjdm=sql_query.value(7).toString();
                       buffer->info.test_syxx=sql_query.value(8).toString();
                       buffer->info.test_sybz=sql_query.value(9).toString();

                       QString sql_data=QString("select test_id,data_t1,data_t2,data_sum,data_tjw,data_bjs,data_jrs,"
                                                "data_sdl,data_zp,data_d1,data_d2 from test_data where test_id='%1' order by test_index asc").arg(test_id);

                       if(sql_query.exec(sql_data))
                       {
                           buffer->data.sql_data.clear();

                           if(sql_query.first())
                           {
                                QStringList sql_frist;
                                for(int i=0;i<11;i++)sql_frist.append(sql_query.value(i).toString());
                                buffer->data.sql_data.append(sql_frist);
                                qDebug()<<sql_frist;
                           }
                           while(sql_query.next())
                           {
                               QStringList sql_next;
                               for(int i=0;i<11;i++)sql_next.append(sql_query.value(i).toString());
                               buffer->data.sql_data.append(sql_next);
                               qDebug()<<sql_next;

                           }


                       }else {
                                  qDebug() << "Error: Fail to select table test_data."<< sql_query.lastError();
                             }
                     }



               }
               else
                   qDebug() << "Error: Fail to select table test_info."<< sql_query.lastError();

           }else
           {
                 qDebug() << "Error: open table test_system.";
           }

           database.close();
           QSqlDatabase::removeDatabase("QSQLITE");//关闭驱动连接
     }


    return 1;
}

int mysql::del_test_ID(QString ID)
{
    QDir dirfile;
    QString file_path=QCoreApplication::applicationDirPath()+QString("/myDb/system_data.db");
    if(dirfile.exists(file_path))
     {
           QSqlDatabase database;
           if(QSqlDatabase::contains("qt_sql_default_connection"))
             database = QSqlDatabase::database("qt_sql_default_connection");
           else
             database = QSqlDatabase::addDatabase("QSQLITE");
           database.setDatabaseName(file_path);

           if(database.open())
           {

               QSqlQuery sql_query(database);

               QString delete_sql=QString("DELETE FROM test_info where test_id='%1'").arg(ID);
               if(sql_query.exec(delete_sql))
                   qDebug() << "DELETE test_info is ok!";

               delete_sql=QString("DELETE FROM test_temp where test_id='%1'").arg(ID);
               if(sql_query.exec(delete_sql))
                   qDebug() << "DELETE test_temp is ok!";

               delete_sql=QString("DELETE FROM test_data where test_id='%1'").arg(ID);
               if(sql_query.exec(delete_sql))
                   qDebug() << "DELETE test_data is ok!";

           }

           database.close();
           QSqlDatabase::removeDatabase("QSQLITE");//关闭驱动连接


     }
    return 1;
}
int mysql::del_test_temp(test_temp data)
{
    QDir dirfile;
    QString file_path=QCoreApplication::applicationDirPath()+QString("/myDb/system_data.db");
    if(dirfile.exists(file_path))
     {
           QSqlDatabase database;
           if(QSqlDatabase::contains("qt_sql_default_connection"))
             database = QSqlDatabase::database("qt_sql_default_connection");
           else
             database = QSqlDatabase::addDatabase("QSQLITE");
           database.setDatabaseName(file_path);

           if(database.open())
           {

               QSqlQuery sql_query(database);

               QString delete_sql=QString("DELETE FROM test_info where test_id='%1' and test_index='%2'").arg(data.test_id).arg(data.test_index);
               if(sql_query.exec(delete_sql))
                   qDebug() << "DELETE test_info is ok!";

               delete_sql=QString("DELETE FROM test_temp where test_id='%1' and test_index='%2'").arg(data.test_id).arg(data.test_index);
               if(sql_query.exec(delete_sql))
                   qDebug() << "DELETE test_temp is ok!";

           }

           database.close();
           QSqlDatabase::removeDatabase("QSQLITE");//关闭驱动连接


     }
    return 1;
}

int mysql::INSERT_test_temp(test_temp data)//插入与更新试验数据
{

    QDir dirfile;
    QString file_path=QCoreApplication::applicationDirPath()+QString("/myDb/system_data.db");
    if(dirfile.exists(file_path))
     {
           QSqlDatabase database;
           if(QSqlDatabase::contains("qt_sql_default_connection"))
             database = QSqlDatabase::database("qt_sql_default_connection");
           else
             database = QSqlDatabase::addDatabase("QSQLITE");
           database.setDatabaseName(file_path);

           if(database.open())
           {

               QSqlQuery sql_query(database);
               QString insert_sql ="INSERT INTO test_temp (test_id,test_index,test_ti,test_tk,test_tn) "
                 "VALUES (:test_id,:test_index,:test_ti,:test_tk,:test_tn)";

                sql_query.prepare(insert_sql);
                sql_query.bindValue(":test_id", QVariant(data.test_id));
                sql_query.bindValue(":test_index", QVariant(data.test_index));
                sql_query.bindValue(":test_ti", QVariant(data.test_ti));
                sql_query.bindValue(":test_tk", QVariant(data.test_tk));
                sql_query.bindValue(":test_tn", QVariant(data.test_tn));

                if(sql_query.exec())
                     qDebug() << "INSERT test_temp is ok!";

           }

           database.close();
           QSqlDatabase::removeDatabase("QSQLITE");//关闭驱动连接


     }
    return 1;
}
int mysql::query_test_temp(test *buffer)
{

    QDir dirfile;
    QString file_path=QCoreApplication::applicationDirPath()+QString("/myDb/system_data.db");
    if(dirfile.exists(file_path))
     {
        QSqlDatabase database;
        if(QSqlDatabase::contains("qt_sql_default_connection"))
          database = QSqlDatabase::database("qt_sql_default_connection");
        else
          database = QSqlDatabase::addDatabase("QSQLITE");
        database.setDatabaseName(file_path);

           if(database.open())
           {

               QSqlQuery sql_query(database);
               QString sql_info=QString("select test_ti,test_tk from test_temp where test_id='%1' and test_index='%2'").arg(buffer->info.test_id).arg(buffer->data.data_index);
               if(sql_query.exec(sql_info))
               {
                   buffer->buf.buf_ci.clear();
                   buffer->buf.buf_ct.clear();

                   if(sql_query.first())
                   {
                       buffer->buf.buf_ci.append(sql_query.value(0).toDouble());
                       buffer->buf.buf_ct.append(sql_query.value(1).toDouble());
                   }

                   while(sql_query.next())
                   {
                       buffer->buf.buf_ci.append(sql_query.value(0).toDouble());
                       buffer->buf.buf_ct.append(sql_query.value(1).toDouble());
                   }

               }
               else
                   qDebug() << "Error: Fail to select table test_info."<< sql_query.lastError();

           }else
           {
                 qDebug() << "Error: open table test_system.";
           }

           database.close();
           QSqlDatabase::removeDatabase("QSQLITE");//关闭驱动连接
     }

    return 1;
}
